/**
 * JSON解析测试文件
 * 用于测试和验证JSON解析功能
 */

import { safeJsonParse, parseActivityData } from './jsonParser'

// 你提供的原始数据
const testData = {
  "id": null,
  "name": "",
  "templateJson": '{"name":"","details":"","isPerson":false,"isBack":false,"titleHeight":0,"bgColor":"rgba(249, 249, 249, 10)","bgImg":""}',
  "component": '[{"component":"pictureads","text":"图片广告","type":"1-3","active":false,"style":"pictureadsstyle","setStyle":{"text":"图片广告","swiperType":0,"borderRadius":0,"pageMargin":0,"imageMargin":0,"pagingType":0,"rowindividual":2,"imageList":[{"src":"https://yqty-1325400698.cos.ap-guangzhou.myqcloud.com/yqty/20250912/e9001eb187118c7dd61ed4f5b7177da8.png","text":"","http":{}}]}},{"component":"captiontext","text":"标题文字","type":"1-3","active":true,"style":"captiontextsstyle","setStyle":{"text":"标题文字","name":"测试活动页面","description":"","wordSize":16,"descriptionSize":12,"wordWeight":400,"positions":"left","descriptionWeight":200,"wordColor":"rgba(50, 50, 51, 10)","descriptionColor":"rgba(150, 151, 153, 10)","backColor":"rgba(255, 255, 255, 10)","borderBott":false,"wordHeight":24,"more":{"show":false,"type":1,"text":"查看更多","httpType":10,"http":""}}}]'
}

// 测试函数
export function testJsonParsing() {
  console.log('=== JSON解析测试开始 ===')
  
  // 测试1: 直接使用 JSON.parse
  console.log('\n1. 直接使用 JSON.parse 解析:')
  try {
    const templateJson = JSON.parse(testData.templateJson)
    const component = JSON.parse(testData.component)
    console.log('✅ templateJson 解析成功:', templateJson)
    console.log('✅ component 解析成功:', component)
  } catch (error) {
    console.error('❌ 直接解析失败:', error.message)
  }
  
  // 测试2: 使用我们的安全解析函数
  console.log('\n2. 使用安全解析函数:')
  try {
    const parsedData = parseActivityData(testData)
    console.log('✅ 安全解析成功:', parsedData)
  } catch (error) {
    console.error('❌ 安全解析失败:', error.message)
  }
  
  // 测试3: 测试单个字段解析
  console.log('\n3. 测试单个字段解析:')
  const templateJsonResult = safeJsonParse(testData.templateJson, {})
  const componentResult = safeJsonParse(testData.component, [])
  console.log('templateJson 解析结果:', templateJsonResult)
  console.log('component 解析结果:', componentResult)
  
  console.log('\n=== JSON解析测试结束 ===')
}

// 导出测试数据
export { testData }

// 如果直接运行此文件，执行测试
if (typeof window !== 'undefined') {
  // 浏览器环境
  window.testJsonParsing = testJsonParsing
} else if (typeof module !== 'undefined' && module.exports) {
  // Node.js 环境
  testJsonParsing()
}
